﻿//-----------------------------------------------------------------
//
// @(#)$Id: EbayAccountDetail.aspx.cs,v 1.0 2013/12/13 17:00:41 lichunhua Exp $
// @(#)$Author: lichunhua  $
// @(#)$Date: 2013/12/13 17:00:41 $
// @(#)$Description:
//
//
//
//                 All Rights Reserved.
//-----------------------------------------------------------------
using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using ebay.Dal.CenterDB_BasTableAdapters;
using ebay.Dal.CenterDB_EbayTableAdapters;
using ebay.Dal.CenterDB_OrderTableAdapters;
using eBay.Service.Core.Soap;

public partial class UI_Bas_EbayAccountDetail : System.Web.UI.Page
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        //OrderFrom
        T_OrderSourceTableAdapter orderSourceAdapter = new T_OrderSourceTableAdapter();
        ddlOrderFrom.DataSource = orderSourceAdapter.GetData();
        ddlOrderFrom.DataTextField = "OrderFromName";
        ddlOrderFrom.DataValueField = "Id";
        ddlOrderFrom.DataBind(); 
                 
        //Modle
        T_ModleTableAdapter modleAdapter = new T_ModleTableAdapter();
        ddlModeId.DataSource = modleAdapter.GetData();
        ddlModeId.DataTextField = "ModleName";
        ddlModeId.DataValueField = "Id";
        ddlModeId.DataBind();
       
        //Payment
        ddlPayment.Items.Insert(0, new ListItem("<--请选择-->", ""));
        ddlPayment.Items.Insert(1, new ListItem("自动扣款", "自动扣款"));
        ddlPayment.Items.Insert(1, new ListItem("手动支付", "手动支付"));

        //AccountStatus
        string[] status = Enum.GetNames(typeof(UserStatusCodeType));
        foreach (string stat in status)
        {
            if (stat != "CustomCode")
            {
                ddlAccountStatus.Items.Add(stat);
            }
        }
        ddlAccountStatus.SelectedIndex = 0;

    }   
    static string act = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {            
            act = Request.QueryString["act"];
            if (!string.IsNullOrEmpty(act))
            {
                switch (act)
                {
                    case "I":
                        break;
                    case "U":                        
                        string EbayUserId = Request.QueryString["id"];
                        txtEbayUserId.Attributes["readonly"] = "readonly"; 
                        T_EbayAccountTableAdapter ebayAdapter = new T_EbayAccountTableAdapter();
                        DataTable dt = ebayAdapter.GetDataByEbayUserId(EbayUserId);
                        DataRow r = dt.Rows[0];
                        if (dt.Rows.Count > 0)
                        {
                            foreach (Control txtControl in this.form1.Controls)
                            {
                                if (txtControl is HtmlInputText)
                                {
                                    HtmlInputText txtBox = (HtmlInputText)txtControl;
                                    string colName = txtBox.ID.Remove(0, 3);
                                    string val = r[colName].ToString();
                                    if (dt.Columns[colName].DataType.Name == "DateTime")
                                    {
                                        DateTime valDate = DateTime.Parse(val);
                                        DateTime defaultDateTime = new DateTime(1900, 1, 1);
                                        if (valDate == defaultDateTime)
                                        {
                                            val = "";
                                        }
                                    }
                                    txtBox.Value = val;
                                }
                                else if (txtControl is HtmlSelect)
                                {
                                    HtmlSelect ddList = (HtmlSelect)txtControl;
                                    string colName = ddList.ID.Substring(3, ddList.ID.Length - 3);
                                    ddList.Value = r[colName].ToString();
                                }
                                else if (txtControl is TextBox)
                                {
                                    TextBox txtbox = (TextBox)txtControl;
                                    string colName = txtbox.ID.Substring(3, txtbox.ID.Length - 3);
                                    txtbox.Text = r[colName].ToString();
                                }
                                else if (txtControl is DropDownList)
                                {
                                    DropDownList ddList = (DropDownList)txtControl;
                                    string colName = ddList.ID.Substring(3, ddList.ID.Length - 3);
                                    ddList.Text = r[colName].ToString();
                                }
                            }
                        }
                        break;
                }
            }          
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Session["UserId"] == null || Session["UserId"].ToString ()=="")
        {
            WebUtility.ShowMsg("登陆超时,请重新登陆", this);
            return;
        }
        string userId = Session["UserId"].ToString();
        if (!string.IsNullOrEmpty(act))
        {  
            switch (act)
            {
                case "I":
                    {
                        T_EbayAccountTableAdapter ebayAdapter = new T_EbayAccountTableAdapter();
                        if (int.Parse(ebayAdapter.IsExists(this.txtEbayUserId.Value).ToString()) == 0)
                        {
                            int val = ebayAdapter.Insert(
                                     txtEbayUserId.Value, txtReturnAddresser.Value, txtCountryNames.Value, txtToken.Text, txtWareHouseAddress.Value,ddlPayment.SelectedValue, txtAccountManagerName.Value,
                                     txtTopOfOrders.Value, int.Parse(txtOrderExpiredDays.Value), ddlAccountStatus.SelectedIndex.ToString(), txtNoteToBuyer.Value, ddlModeId.Value.ToString (),
                                     txtCustomerServicePhone.Value, txtAccountManagerNote.Value, txtAccountManagerEmail.Value, ddlOrderFrom.Value.ToString (), int.Parse(txtOrderKeepDays.Value),
                                     txtRegisterAddress.Value, txtDefaultRegisterPhone.Value, txtRegisterPhone1.Value, txtRegisterPhone2.Value, txtRegisterPhone3.Value, txtRegisterPhone4.Value,
                                     txtRegisterPhone5.Value, txtRegisterPhone6.Value, txtRegisterPhone7.Value, txtDefaultRegisterAddresser.Value, txtRegisterAddresser1.Value, txtRegisterAddresser2.Value,
                                     txtRegisterAddresser3.Value, txtRegisterAddresser4.Value, txtRegisterAddresser5.Value, txtRegisterAddresser6.Value, txtRegisterAddresser7.Value, txtName.Value,
                                     txtBusinessNameAndInfo.Value, txtRetrunAddresser.Value, txtAccountManager.Value, txtRegisterCreditCard.Value, txtIPAddresser.Value, txtIPAddresserCompany.Value,
                                     txtMnote.Value,
                                     userId,
                                     DateTime .Now ,
                                     "",
                                     new DateTime(1900, 1, 1));
                            if (val > 0)
                            {
                                Response.Write("<script>alert('添加成功!');window.location.href='EbayAccount.aspx'</script>");
                            }
                        }
                        else
                        {
                            Response.Write("<script>alert('Ebay帐号相同，请查看！');window.location.href='EbayAccount.aspx'</script>");
                        }
                    }
                    break;
                case "U":
                    {
                        string EbayUserId = Request.QueryString["id"];
                        T_EbayAccountTableAdapter ebayAdapter = new T_EbayAccountTableAdapter();
                        DataTable dt = ebayAdapter.GetDataByEbayUserId(EbayUserId);
                        if (dt.Rows.Count > 0)
                        {
                            DataRow r = dt.Rows[0];
                            foreach (Control txtControl in this.form1.Controls)
                            {
                                if (txtControl is HtmlInputText)
                                {
                                    HtmlInputText txtBox = (HtmlInputText)txtControl;
                                    string colName = txtBox.ID.Substring(3, txtBox.ID.Length - 3);
                                    if (txtBox.Value != r[colName].ToString())
                                    {
                                        r[colName] = txtBox.Value;
                                    }
                                }
                                else if (txtControl is HtmlSelect)
                                {
                                    HtmlSelect ddList = (HtmlSelect)txtControl;
                                    string colName = ddList.ID.Substring(3, ddList.ID.Length - 3);
                                    if (ddList.Value != r[colName].ToString())
                                    {
                                        r[colName] = ddList.Value;
                                    }
                                }
                                else if (txtControl is TextBox)
                                {
                                    TextBox txtbox = (TextBox)txtControl;
                                    string colName = txtbox.ID.Substring(3, txtbox.ID.Length - 3);                                    
                                    r[colName] = txtbox.Text;
                                }
                            }
                            r["Payment"] = ddlPayment.SelectedValue;
                            r["Muser"] = userId;
                            r["Mtime"] = DateTime.Now;

                            int val = ebayAdapter.Update(dt.Rows[0]);
                            if (val > 0)
                            {
                                Response.Write("<script>alert('修改成功!');window.location.href='EbayAccount.aspx'</script>");
                            }
                        }
                    }
                    break;
                default:
                    break;
            }
        }
    }  
}
